/*
 * Transport functional.
 * Responsible for client-server transfer
 */
 
var baseUrl = window.location.pathname + "?operation=game&action=";
var url = window.location.pathname + "go?operation=game&action=";

var PLAYER_WINS = 1;
var COMPUTER_WINS = 2;

function doStart() {
	$.ajax({
 		url: baseUrl + "start",
 		success: function (data) {
			// do smth
 		}
	});	
}

function doFinish() {
	$.ajax({
 		url: baseUrl + "finish",
 		success: function (data) {
			// do smth
 		}
	});	
}
 
function doTurn(x, y) {
 	var position = new Object();
 	position.x = x;
 	position.y = y;
 	
 	$.ajax({
 		url: baseUrl + "turn&data=" + $.toJSON(position),
 		success: function (data) {
 			var evalData = $.evalJSON(data);
 			refreshBoard(evalData.cellStates);
 			if (PLAYER_WINS == evalData.state) {
 				alert("PLAYER WINS");
 			} else if (COMPUTER_WINS == evalData.state) {
 				alert("COMPUTER_WINS");
 			}
 			//updateMessage(evalData.state);
 		}
	});	
}


function doRefresh() {
	$.ajax({
 		url: baseUrl + "refresh",
 		success: function (data) {
			var evalData = $.evalJSON(data);
 			refreshBoard(evalData.cellStates);
 			updateMessage(evalData.message);
 		}
	});	
}

function getGamesListHTML() {
	$.ajax({
		url: url + "getGamesList",
		success: function (data) {
			var evalData = $.evalJSON(data);
			updateGamesList(evalData.list);
		}
	});
	
}
